﻿using DistDataProcessor.Data;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DistDataProcessor.Excel
{
    public class DistDataExporter
    {
        /// <summary>
        /// 导出所有的测距仪数据到Excel表格
        /// </summary>
        /// <param name="datas">测距仪数据列表</param>
        /// <returns>出错的信息,如果没出错,返回空字符串</returns>
        public static string Export(List<IGrouping<int,MeasureDistanceData>> datas)
        {
            var path = DistDataExporter.GetUsbDeviceDirectory();
            if (path == "") return "没有可用的USB设备!";
            var sheetName = "测距仪数据_" + DateTime.Now.ToString("yyyy_MM_dd") + ".xlsx";
            DistDataExporter.CreateExcel(path + sheetName,datas, "测距仪数据");
            return "";
        }


        /// <summary>
        /// 创建一个Excel文件到指定路径
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="sheetName">sheet名字,默认为sheet</param>
        private static void CreateExcel(string path, List<IGrouping<int, MeasureDistanceData>> datas, string sheetName = "sheet")
        {
            using (var writeSteam = new FileStream(path, FileMode.Create))
            {
                var book = new XSSFWorkbook();
                ISheet sheet = book.CreateSheet(sheetName);

                //输出仪器编号
                if (datas.Count > 0)
                {
                    var cell = sheet.GetCell(3, 0, true);
                    //cell.Offset(-1, 0).SetCellValue("时间");
                    //cell.Offset(-2, 0).SetCellValue("日期");
                    //cell.Offset(-3, 0).SetCellValue("次数");
                    var d = datas[0];
                    foreach (var p in d)
                    {
                        cell.SetCellValue(p.Id);
                        cell = cell.Offset(1, 0);
                    }
                }
                for (var i = 0; i < datas.Count; i++)
                {
                    var cell = sheet.GetCell(0, i + 1, true);
                   
                    cell.SetCellValue(datas[i].Key);
                    cell = cell.Offset(1, 0);
                    cell.SetCellValue(datas[i].First().Time.ToString("yyyy-MM-dd"));
                    cell = cell.Offset(1, 0);
                    cell.SetCellValue(datas[i].First().Time.ToLongTimeString());
                    foreach (var p in datas[i])
                    {
                        cell = cell.Offset(1, 0);
                        cell.SetCellValue(p.Distance);
                    }
                    cell.Sheet.AutoSizeColumn(cell.ColumnIndex);
                }
                book.Write(writeSteam);
            }
        }

        /// <summary>
        /// 获取U盘盘符路径
        /// </summary>
        /// <returns>盘符字符串,如"G:/"</returns>
        private static string GetUsbDeviceDirectory()
        {
            var drives = DriveInfo.GetDrives()
                .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable).ToList();

            if (drives.Count > 0)
            {
                return drives[0].RootDirectory.FullName;
            }
            else
            {
                return "";
            }

        }
    }
}
